package cz.fimuni.pa165.backendderby.services;

import cz.fimuni.pa165.backendapi.dto.CompanyDTO;
import cz.fimuni.pa165.backendapi.dto.RequestDTO;
import cz.fimuni.pa165.backendderby.dao.CarDAO;
import cz.fimuni.pa165.backendderby.entities.Car;
import java.util.List;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Ondřej Myška
 */
@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarDAO carDao;
    @Autowired
    private Mapper mapper;

    @Transactional
    public void createCar(Car car) {

        carDao.createCar(car);

    }

    @Transactional
    public void deleteCar(Car car) {

        carDao.remove(car);

    }

    @Transactional
    public List<Car> getCar(Car car) {

        return carDao.getCar(car.getSpz());

    }

    @Transactional
    public boolean updateCar(Car car) {

        return carDao.update(car);

    }

    @Transactional
    public List<Car> getAllCars() {

        return carDao.getAllCars();

    }

    @Transactional
    public Car find(Long id) {
        return carDao.find(id);
    }

    @Transactional
    public List<Car> processRequest(RequestDTO request) {
        String[] types = request.getTypes();
        String[] colours = request.getColours();
        int[] numbersOfSeats = request.getNumbersOfseats();
        CompanyDTO company = request.getUsersCompany();
        String queryString = createQueryString(types, colours, numbersOfSeats, company);

        List<Car> list = null;

        list = carDao.processQuery(queryString);

        return list;
    }

    public String createQueryString(String[] types, String[] colours,
            int[] numbersOfSeats, CompanyDTO usersCompany) {
        String queryString = "SELECT c FROM Car c WHERE";


        if (types != null) {
            queryString += " ( ";
            for (int i = 0; i < types.length; i++) {
                String str = types[i];
                queryString += "c.type='" + str + "'";
                if (i != types.length - 1) {
                    queryString += " OR ";
                }
            }
            queryString += " ) ";
        }

        if (colours != null) {
            if(types!=null)queryString += "AND";
            queryString += " ( ";
            for (int i = 0; i < colours.length; i++) {
                String str = colours[i];
                queryString += "c.colour='" + str + "'";
                if (i != colours.length - 1) {
                    queryString += " OR ";
                }
            }
            queryString += " ) ";
        }

        if (numbersOfSeats != null) {
            if(types!=null || colours!=null)queryString += "AND";
            queryString += " ( ";
            for (int i = 0; i < numbersOfSeats.length; i++) {
                int cislo = numbersOfSeats[i];
                queryString += "c.numberOfSeats=" + cislo ;
                if (i != numbersOfSeats.length - 1) {
                    queryString += " OR ";
                }
            }
            queryString += " ) ";
        }

        if(usersCompany!=null)queryString += " c.company=" + usersCompany;


        return queryString;
    }

    
}
